package com.support.SQLite;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

public class BaseDao {
	SQLiteDatabase db;
	SQLHelper dbHelper;

	public void init(Context context) {
		dbHelper = new SQLHelper(context);
		db = dbHelper.getWritableDatabase();
	}

	public long insert(String TAB_Name, ContentValues values) {
		if (db == null || values == null || TextUtils.isEmpty(TAB_Name)) {
			Log.e("BAseDao", "something is empty or null");
			return -1;
		}
	
		return db.insert(TAB_Name, null, values);
	}

	/**
	 * the number of rows affected if a whereClause is passed in, 0 otherwise.
	 * To remove all rows and get a count pass "1" as the whereClause.
	 */
	public int delete(String TabName, long DB_id) {
		return db.delete(TabName, SQLHelper.COLUMN_ID + "=" + DB_id, null);
	}

	public int update(String tabName, ContentValues values, String where) {
		return db.updateWithOnConflict(tabName, values, where, null, SQLiteDatabase.CONFLICT_ROLLBACK);
	}

	/**
	 * @ distinct --是否去除重复行 例：值为：true/false; * @ table --表名 * @ columns --要查询的列
	 * 例： new String[]{"id","name","age"} * @ selection --查询条件 例："id>" * @
	 * selectionArgs --查询条件的参数 例：new String[]{"3"}
	 * 
	 * @ groupBy --对查询的结果进行分组
	 * 
	 * @ having --对分组的结果进行限制
	 * 
	 * @ orderby --对查询的结果进行排序； 例："age asc"
	 * 
	 * @ limit --分页查询限制 ； 例："2,5" 从第2行开始，到第5行结束；注：行数从0 开始； // Cursor cursor =
	 * sqliteDatabase.query(true,"student", new // String[]{"_id","name","age"},
	 * "_id>", new String[]{"1"}, null, null, // "age desc", "1,5");
	 */
	public Cursor query(String tabName, String[] columns, String where, String groupBy, String having, String orderBy,
			String limit) {
		return db.query(true, tabName, columns, where, null, groupBy, having, orderBy, limit);
	}

	public Cursor query(String tabName, String[] columns) {
		return db.query(true, tabName, columns, null, null, null, null, null, null);
	}

}
